package com.youi.third.wechat.model.message;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.youi.third.wechat.util.WechatUtil;
import com.youi.util.LogUtil;

/**
 * 微信模版消息工具类
 * @author erdong.Chan
 *
 */
public class WeCharModelMessageUtil {
	
	
	private static String toUrl;
	
	
	
	public WeCharModelMessageUtil(String toUrl) {
		this.toUrl = toUrl;
	}

	/**
	 * 发送发货成功模板消息
	 * @return
	 */
	public static String toSendOrderModelMessage(String openId,String expressCompany,String expressNum){
		String accessToken=WechatUtil.getAccessToken();
		if(accessToken.length()<=0){
			return "";
		}
		System.out.println("accessToken==="+accessToken);
		String urlStr="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+accessToken;
		try {
			URL url = new URL(urlStr);
			HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
			urlConnection.setDoOutput(true);
			urlConnection.setDoInput(true);
			urlConnection.setRequestMethod("POST");
			urlConnection.setUseCaches(false); 
			urlConnection.setInstanceFollowRedirects(true);
			urlConnection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
			urlConnection.setRequestProperty("Charset", "UTF-8"); 
			urlConnection.connect();
			DataOutputStream out = new DataOutputStream(urlConnection.getOutputStream());
			String params="{"+ 
							"\"touser\":\""+openId+"\","+
							"\"template_id\":\"ysx6-KR27DZSRizwkwCnbpvsJnvaBuUm90Tr5MPmL5Y\","+
							"\"url\":\""+toUrl+"\","+ 
							"\"data\":{"+
								"\"first\": {"+
								"\"value\":\"亲,您的订单已被处理!\","+
								"\"color\":\"#173177\""+
								"},"+
								"\"delivername\": {"+
								"\"value\":\""+expressCompany+"\","+
								"\"color\":\"#173177\""+
								"},"+
								"\"ordername\": {"+
								"\"value\":\""+expressNum+"\","+
								"\"color\":\"#173177\""+
								"},"+
								"\"remark\":{"+
								"\"value\":\"点击查看订单\","+
								"\"color\":\"#173177\""+
								"}"+
							 "}"+
							"}";		
			out.write(params.getBytes("UTF-8"));
	        out.flush();
	        out.close(); 
	        BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(),"UTF-8"));
	        String line;
	        String responseStr="";
	        while ((line = reader.readLine()) != null){
	        	responseStr+=line;
	        }
	        reader.close();
	        urlConnection.disconnect();
	        LogUtil.Info("模版消息调用结果:"+responseStr);
		}catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	}
	
	
	/**
	 * 发送服务受理模板消息
	 * @return
	 */
	public static String toSendServerModelMessage(String openId,String title,String servertype,String servertime,String bootom){
		String accessToken=WechatUtil.getAccessToken();
		if(accessToken.length()<=0){
			return "";
		}
		System.out.println("accessToken==="+accessToken);
		String urlStr="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+accessToken;
		try {
			URL url = new URL(urlStr);
			HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
			urlConnection.setDoOutput(true);
			urlConnection.setDoInput(true);
			urlConnection.setRequestMethod("POST");
			urlConnection.setUseCaches(false); 
			urlConnection.setInstanceFollowRedirects(true);
			urlConnection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
			urlConnection.setRequestProperty("Charset", "UTF-8"); 
			urlConnection.connect();
			DataOutputStream out = new DataOutputStream(urlConnection.getOutputStream());
			String params="{"+ 
							"\"touser\":\""+openId+"\","+
							"\"template_id\":\"JU_efab7hmkYKUhl9R-6xKQfNhnOAI_DQp66HWIRhPA\","+
							"\"url\":\"\","+ 
							"\"data\":{"+
								"\"first\": {"+
								"\"value\":\""+title+"\","+
								"\"color\":\"#173177\""+
								"},"+
								"\"keyword1\": {"+
								"\"value\":\""+servertype+"\","+
								"\"color\":\"#173177\""+
								"},"+
								"\"keyword2\": {"+
								"\"value\":\""+servertime+"\","+
								"\"color\":\"#173177\""+
								"},"+
								"\"remark\":{"+
								"\"value\":\""+bootom+"\","+
								"\"color\":\"#173177\""+
								"}"+
							 "}"+
							"}";		
			out.write(params.getBytes("UTF-8"));
	        out.flush();
	        out.close(); 
	        BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(),"UTF-8"));
	        String line;
	        String responseStr="";
	        while ((line = reader.readLine()) != null){
	        	responseStr+=line;
	        }
	        reader.close();
	        urlConnection.disconnect();
	        LogUtil.Info("模版消息调用结果:"+responseStr);
	        return responseStr;
		}catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	}
	
	
	/**
	 * 发送服务状态模板消息
	 * @return
	 */
	public static String toSendServerStateModelMessage(String openId,String title,String serverName,String serverProgress,String bootom){
		String accessToken=WechatUtil.getAccessToken();
		if(accessToken.length()<=0){
			return "";
		}
		System.out.println("accessToken==="+accessToken);
		String urlStr="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+accessToken;
		try {
			URL url = new URL(urlStr);
			HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
			urlConnection.setDoOutput(true);
			urlConnection.setDoInput(true);
			urlConnection.setRequestMethod("POST");
			urlConnection.setUseCaches(false); 
			urlConnection.setInstanceFollowRedirects(true);
			urlConnection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
			urlConnection.setRequestProperty("Charset", "UTF-8"); 
			urlConnection.connect();
			DataOutputStream out = new DataOutputStream(urlConnection.getOutputStream());
			String params="{"+ 
							"\"touser\":\""+openId+"\","+
							"\"template_id\":\"n0erx-KXUbV-bnBryEM_DAkj20HQS-8EZaTyhCdOBMM\","+
							"\"url\":\"\","+ 
							"\"data\":{"+
								"\"first\": {"+
								"\"value\":\""+title+"\","+
								"\"color\":\"#173177\""+
								"},"+
								"\"keyword1\": {"+
								"\"value\":\""+serverName+"\","+
								"\"color\":\"#173177\""+
								"},"+
								"\"keyword2\": {"+
								"\"value\":\""+serverProgress+"\","+
								"\"color\":\"#173177\""+
								"},"+
								"\"remark\":{"+
								"\"value\":\""+bootom+"\","+
								"\"color\":\"#173177\""+
								"}"+
							 "}"+
							"}";		
			out.write(params.getBytes("UTF-8"));
	        out.flush();
	        out.close(); 
	        BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(),"UTF-8"));
	        String line;
	        String responseStr="";
	        while ((line = reader.readLine()) != null){
	        	responseStr+=line;
	        }
	        reader.close();
	        urlConnection.disconnect();
	        LogUtil.Info("模版消息调用结果:"+responseStr);
	        return responseStr;
		}catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	}
	
	/**
	 * 积分变动模板消息
	 * @return
	 */
	public static String scoreChangeModelMessage(String openId,String title,String userName,String time,String score,Integer scoreAmount,String reason,String bootom){
		String accessToken=WechatUtil.getAccessToken();
		if(accessToken.length()<=0){
			return "";
		}
		System.out.println("accessToken==="+accessToken);
		String urlStr="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+accessToken;
		try {
			URL url = new URL(urlStr);
			HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
			urlConnection.setDoOutput(true);
			urlConnection.setDoInput(true);
			urlConnection.setRequestMethod("POST");
			urlConnection.setUseCaches(false); 
			urlConnection.setInstanceFollowRedirects(true);
			urlConnection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
			urlConnection.setRequestProperty("Charset", "UTF-8"); 
			urlConnection.connect();
			DataOutputStream out = new DataOutputStream(urlConnection.getOutputStream());
			String params="{"+ 
							"\"touser\":\""+openId+"\","+
							"\"template_id\":\"ON18NoRW_114O45dltvrQodiJSi6WRfzPn5ZHVGnOXI\","+
							"\"url\":\"\","+ 
							"\"data\":{"+
								"\"first\": {"+
								"\"value\":\""+title+"\","+
								"\"color\":\"#173177\""+
								"},"+
								"\"keyword1\": {"+
								"\"value\":\""+userName+"\","+
								"\"color\":\"#173177\""+
								"},"+
								"\"keyword2\": {"+
								"\"value\":\""+time+"\","+
								"\"color\":\"#173177\""+
								"},"+
								"\"keyword3\": {"+
								"\"value\":\""+score+"\","+
								"\"color\":\"#173177\""+
								"},"+
								"\"keyword4\": {"+
								"\"value\":\""+scoreAmount+"\","+
								"\"color\":\"#173177\""+
								"},"+
								"\"keyword5\": {"+
								"\"value\":\""+reason+"\","+
								"\"color\":\"#173177\""+
								"},"+
								"\"remark\":{"+
								"\"value\":\""+bootom+"\","+
								"\"color\":\"#173177\""+
								"}"+
							 "}"+
							"}";		
			out.write(params.getBytes("UTF-8"));
	        out.flush();
	        out.close(); 
	        BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(),"UTF-8"));
	        String line;
	        String responseStr="";
	        while ((line = reader.readLine()) != null){
	        	responseStr+=line;
	        }
	        reader.close();
	        urlConnection.disconnect();
	        LogUtil.Info("模版消息调用结果:"+responseStr);
	        return responseStr;
		}catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	}
	
	/**
	 * 订单状态更新模板消息
	 * @return
	 */
	public static String orderChangeModelMessage(String openId,String title,String orderNumber,String orderState,String bootom){
		String accessToken=WechatUtil.getAccessToken();
		if(accessToken.length()<=0){
			return "";
		}
		System.out.println("accessToken==="+accessToken);
		String urlStr="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+accessToken;
		try {
			URL url = new URL(urlStr);
			HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
			urlConnection.setDoOutput(true);
			urlConnection.setDoInput(true);
			urlConnection.setRequestMethod("POST");
			urlConnection.setUseCaches(false); 
			urlConnection.setInstanceFollowRedirects(true);
			urlConnection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
			urlConnection.setRequestProperty("Charset", "UTF-8"); 
			urlConnection.connect();
			DataOutputStream out = new DataOutputStream(urlConnection.getOutputStream());
			String params="{"+ 
							"\"touser\":\""+openId+"\","+
							"\"template_id\":\"LpY58mwJM017bUSNU0azTU1avSesamZfUSmvJmCC_Qg\","+
							"\"url\":\"\","+ 
							"\"data\":{"+
								"\"first\": {"+
								"\"value\":\""+title+"\","+
								"\"color\":\"#173177\""+
								"},"+
								"\"OrderSn\": {"+
								"\"value\":\""+orderNumber+"\","+
								"\"color\":\"#173177\""+
								"},"+
								"\"OrderStatus\": {"+
								"\"value\":\""+orderState+"\","+
								"\"color\":\"#173177\""+
								"},"+
								"\"remark\":{"+
								"\"value\":\""+bootom+"\","+
								"\"color\":\"#173177\""+
								"}"+
							 "}"+
							"}";		
			out.write(params.getBytes("UTF-8"));
	        out.flush();
	        out.close(); 
	        BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(),"UTF-8"));
	        String line;
	        String responseStr="";
	        while ((line = reader.readLine()) != null){
	        	responseStr+=line;
	        }
	        reader.close();
	        urlConnection.disconnect();
	        LogUtil.Info("模版消息调用结果:"+responseStr);
	        return responseStr;
		}catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	}
}
